package jsyf.oa.service.report;

import com.alibaba.fastjson.JSONObject;
import jsyf.com.util.FastJsonConvert;
import jsyf.oa.dao.report.PersonnelDao;
import jsyf.oa.dao.system.SignInDao;
import jsyf.oa.facade.report.PersonnelFacade;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by Administrator on 2018/2/24 0024.
 */
@Service
@Transactional(rollbackFor = Exception.class)
@com.alibaba.dubbo.config.annotation.Service(protocol = {"dubbo"})
public class PersonnelService implements PersonnelFacade {
    @Autowired
    private PersonnelDao personnelDao;
    @Autowired
    private SignInDao signInDao;

    private static String one = "1";
    private static String two = "2";

    @Override
    public JSONObject getByUserId(String jsonData) {
        JSONObject result = new JSONObject();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String companyId = jsonParams.getString("companyId");
        String departId = jsonParams.getString("departId");
        String type = jsonParams.getString("type");
        Date dayCurrent = personnelDao.getCurrentTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateTime = sdf.format(dayCurrent);
        if (one.equals(type)) {
            result = personnelDao.getGroupByManager(dateTime);
        } else if (two.equals(type)) {
            if (StringUtils.isNotBlank(companyId)) {
                result = personnelDao.getCompanyByManager(dateTime, companyId);
            }
        } else {
            if (StringUtils.isNotBlank(userId)) {
                result = personnelDao.getRegionByManager(dateTime, userId, departId);
            }
        }
        return result;
    }

    @Override
    public List<JSONObject> getCompanyByManage() {
        return personnelDao.getCompanyByManage();
    }

    @Override
    public List<JSONObject> getSaleByManage(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String companyId = jsonParams.getString("companyId");
        return personnelDao.getSaleByManage(companyId);
    }

    @Override
    public List<JSONObject> getRegionByManager(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String departId = jsonParams.getString("departId");
        //获取某个大区经理和区域经理
        List<JSONObject> regionlist = signInDao.getRegionListByBigRegionDepartId(departId);
        //返回的集合
        List<JSONObject> list = new ArrayList<>();
        for (Integer i = 0; i < regionlist.size(); i++) {
            JSONObject objResult = new JSONObject();
            String regionId = regionlist.get(i).get("regionId").toString();
            String managerId = regionlist.get(i).get("managerId").toString();
            String managerRoleName = regionlist.get(i).get("managerRoleName").toString();
            String managerName = regionlist.get(i).get("managerName").toString();
            String regionName = regionlist.get(i).get("regionName").toString();
            String managerJobState = regionlist.get(i).get("managerJobState").toString();
            String bigRegionName = regionlist.get(i).get("bigRegionName").toString();
            if (regionName.contains("大区")) {
                objResult.put("bigRegionName", regionName.replace("大区", ""));
            } else {
                objResult.put("bigRegionName", bigRegionName);
            }
            objResult.put("regionId", regionId);
            objResult.put("regionName", regionName);
            objResult.put("managerId", managerId);
            objResult.put("managerRoleName", managerRoleName);
            objResult.put("managerName", managerName);
            objResult.put("managerJobState", managerJobState);

            list.add(objResult);
        }
        return list;
    }

    @Override
    public List<JSONObject> getSupportRegionList(String jsonData) {
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        //获取某个大区经理和区域经理
        List<JSONObject> list = new ArrayList<>();
        List<JSONObject> regionList = signInDao.getSupportRegionList(userId);
        for (JSONObject valJson : regionList.stream().filter(x -> x.get("parentId").toString().equals("0")).collect(Collectors.toList())) {
            if (valJson.get("regionName").equals("大客户")) {
                continue;
            }
            for (JSONObject regionJson : regionList.stream().filter(x -> x.get("parentId").toString().equals(valJson.get("regionId")) || x.get("regionId").toString().equals(valJson.get("regionId"))).collect(Collectors.toList())) {
                JSONObject json = new JSONObject();
                String uid = regionJson.get("userId").toString();
                if (regionJson.get("parentId").toString().equals("0")) {
                    json.put("departName", regionJson.get("regionName").toString());
                } else if (!regionJson.get("parentId").toString().equals("")) {
                    json.put("departName", regionList.stream().filter(x -> x.get("regionId").toString().equals(regionJson.get("parentId").toString())).collect(Collectors.toList()).get(0).get("regionName").toString());
                }
                json.put("regionName", regionJson.get("regionName").toString());
                json.put("roleName", regionJson.get("parentId").toString().equals("0") ? "大区经理" : "区域经理");
                json.put("name", !regionJson.get("userName").toString().equals("") ? regionJson.get("userName").toString() : "空缺");
                json.put("userId", uid);
                json.put("jobState", regionJson.getString("jobState"));
                list.add(json);
            }
        }
        return list;
    }

    @Override
    public List<JSONObject> getEntryById(String jsonData) {
        List<JSONObject> result = new ArrayList<>();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String companyId = jsonParams.getString("companyId");
        String departId = jsonParams.getString("departId");
        String type = jsonParams.getString("type");
        Date dayCurrent = personnelDao.getCurrentTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateTime = sdf.format(dayCurrent);
        if (one.equals(type)) {
           result = personnelDao.getGroupByManagerEntry(dateTime);
        } else if (two.equals(type)) {
            if (StringUtils.isNotBlank(companyId)) {
                result = personnelDao.getCompanyByManagerEntry(dateTime, companyId);
            }
        } else {
            if (StringUtils.isNotBlank(userId)) {
              result = personnelDao.getRegionByManagerEntry(dateTime, userId, departId);
            }
        }
        return result;
    }

    @Override
    public List<JSONObject> getLeaveById(String jsonData) {
        List<JSONObject> result = new ArrayList<>();
        JSONObject jsonParams = FastJsonConvert.convertJSONToObject(jsonData, JSONObject.class);
        String userId = jsonParams.getString("userId");
        String companyId = jsonParams.getString("companyId");
        String departId = jsonParams.getString("departId");
        String type = jsonParams.getString("type");
        Date dayCurrent = personnelDao.getCurrentTime();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String dateTime = sdf.format(dayCurrent);
        if (one.equals(type)) {
            result = personnelDao.getGroupByManagerLea(dateTime);
        } else if (two.equals(type)) {
            if (StringUtils.isNotBlank(companyId)) {
                result = personnelDao.getCompanyByManagerLea(dateTime, companyId);
            }
        } else {
            if (StringUtils.isNotBlank(userId)) {
                result = personnelDao.getRegionByManagerLea(dateTime, userId, departId);
            }
        }
        return result;
   }


}
